﻿using System.Text;

namespace lie5
{
    internal class Program3
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding=Encoding.UTF8;
            int w=50,h=30;
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h);
            int scene = 1;//场景编号
            int option = 0;//选项编号
            Console.CursorVisible = false;//隐藏光标
            while (true)
            {
                switch (scene)
                {
                    case 1:
                        Console.Clear();//清除上个场景内容
                        Console.SetCursorPosition(w / 2 - 11, 8);//w/2-7是因为除2会将文字放到中心位置，减11是因为光标在第一个字，一共有七个字，要移动到中心位置
                        Console.Write("Charlie勇士挑战小怪兽");
                        //给开始界面构造一个循环
                        int number = 0;
                        bool exit = false;
                        //用一个标识  来处理 想要在while循环内部的switch逻辑执行时 希望退出外层while循环时改变标识即可
                        while (true)
                        {
                            //显示  内容
                            //先设置光标再显示内容
                            Console.ForegroundColor = number == 0 ? ConsoleColor.DarkBlue : ConsoleColor.White;//三目运算符
                            Console.SetCursorPosition(w / 2 - 4, 11);
                            Console.Write("开始游戏");
                            Console.ForegroundColor = number == 1 ? ConsoleColor.DarkBlue : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 13);
                            Console.Write("结束游戏");
                            char input = Console.ReadKey(true).KeyChar;
                            switch (input)
                            {
                                case 'w':
                                case 'W':
                                    --number;
                                    if (number < 0)
                                    {
                                        number = 0;
                                    }
                                    break;
                                case 's':
                                case 'S':
                                    ++number;
                                    if (number > 1)
                                    {
                                        number = 1;
                                    }
                                    break;
                                case 'j':
                                case 'J':
                                    if (number == 0)
                                    {
                                        //改变当前选择的场景编号
                                        scene = 2;
                                        //退出内层循环
                                        exit = true;
                                    }
                                    else
                                    {
                                        Environment.Exit(0);
                                    }
                                    break;
                            }
                            if (exit)//放外面，这样才能直接退出while循环
                            {
                                break;
                            }
                        }
                        break;
                    case 2:
                        Console.Clear();
                       
                        //设置颜色
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        //画墙
                        //上方墙
                        for (int i = 0; i < w; i += 2)
                        {
                            //上方墙
                            Console.SetCursorPosition(i, 0);
                            Console.Write("■");
                            //下方墙
                            Console.SetCursorPosition(i, h - 1);//顶格不能完全相等（原点除外）
                            Console.Write("■");
                            //中间墙
                            Console.SetCursorPosition(i, h - 6);//比下方墙高六格
                            Console.Write("■");
                        }
                        for (int i = 0; i < h; i++)
                        {
                            //左边的墙
                            Console.SetCursorPosition(0, i);
                            Console.Write("■");
                            //右边的墙
                            Console.SetCursorPosition(w - 2, i);//顶格不能相等
                            Console.Write("■");
                        }
                        //设计boss相关
                        int bossX = 36, bossY = 16;//坐标
                        int bossAtkMin = 5, bossAtkMax = 10;//最大攻击力和最小攻击力
                        int bossHP = 100;
                        string bossIcon = "怪";//怪物形状
                        ConsoleColor bossColor = ConsoleColor.DarkRed;//申明一个颜色变量

                        //设计主角相关
                        int playerX = 4, playerY = 5;
                        int playerAtkMin = 10, playerAtkMax = 15;
                        int playerHP = 100;
                        string playerIcon = "我";
                        ConsoleColor playerColor = ConsoleColor.Blue;
                        //bool over=false;
                        bool over=false;
                        bool fight = false;//定义一个bool值，当主角进入战斗状态将无法移动（要放到循环外面，不然赋值后又会变回去）
                        //游戏场景的死循环，用来检测玩家输入相关
                        while (true)//我真服了我靠，挑半天错不知道怎么改变老闪结果就这个空循环不小心跳过了没看见浪费了我差不多三个多小时啊啊啊啊啊我恨啊啊啊啊啊啊啊啊啊啊啊啊啊
                        {
                            if (bossHP > 0)
                            {
                                Console.SetCursorPosition(bossX, bossY);
                                Console.ForegroundColor = bossColor;
                                Console.Write(bossIcon);
                            }
                            
                                Console.SetCursorPosition(playerX, playerY);
                                Console.ForegroundColor = playerColor;
                                Console.Write(playerIcon);
                                //bool fight = false;//定义一个bool值，当主角进入战斗状态将无法移动
                                char a = Console.ReadKey(true).KeyChar;
                                
                                if (fight)
                                { 
                                    //战斗执行代码
                                    if (a == 'j' || a == 'J')
                                    {
                                         
                                        //回合制
                                        //玩家回合
                                        Random p = new Random();
                                        int atk=p.Next(playerAtkMin, playerAtkMax);
                                        bossHP-=atk;
                                        Console.ForegroundColor = ConsoleColor.Gray;
                                        Console.SetCursorPosition(3, h - 4);
                                        Console.WriteLine("Charlie攻击力为{0}，boss血量还剩{1}",atk,bossHP);
                                        //char b=Console.ReadKey(true).KeyChar;

                                        //boss回合
                                        if (bossHP > 0)
                                        {
                                            Random p2 = new Random();
                                            int atk2 = p2.Next(bossAtkMin, bossAtkMax);
                                            playerHP -= atk2;
                                            Console.ForegroundColor = ConsoleColor.Gray;
                                            Console.SetCursorPosition(3, h - 3);
                                            Console.WriteLine("boss攻击力为{0}，Charlie血量还剩{1}", atk2, playerHP);
                                        }
                                        else
                                        {
                                            Console.SetCursorPosition(3,h-4);
                                            Console.WriteLine("boss已被打败！                       ");
                                            char c=Console.ReadKey().KeyChar;
                                            scene = 3;
                                            over=true;
                                        }
                                    }
                                }
                                else
                                {
                                    Console.SetCursorPosition(playerX, playerY);
                                    Console.Write(' ');
                                    switch (a)
                                    {
                                        case 'w':
                                        case 'W':
                                            --playerY;
                                            if (playerY < 1)
                                            {
                                                playerY = 1;
                                            }
                                            else if (playerX == bossX && playerY == bossY && bossHP > 0)
                                            {
                                                ++playerY;//碰到boss后被拉回去
                                            }
                                            break;
                                        case 's':
                                        case 'S':

                                            ++playerY;
                                            if (playerY > h - 7)
                                            {
                                                playerY = h - 7;
                                            }
                                            else if (playerX == bossX && playerY == bossY && bossHP > 0)
                                            {
                                                playerY--;//碰到boss后被拉回去
                                            }
                                            break;
                                        case 'a':
                                        case 'A':
                                            playerX -= 2;
                                            if (playerX < 2)
                                            {
                                                playerX = 2;
                                            }
                                            else if (playerX == bossX && playerY == bossY && bossHP > 0)
                                            {
                                                playerX += 2;//碰到boss后被拉回去
                                            }
                                            break;
                                        case 'd':
                                        case 'D':
                                            playerX += 2;
                                            if (playerX > w - 4)
                                            {
                                                playerX = w - 4;
                                            }
                                            else if (playerX == bossX && playerY == bossY && bossHP > 0)
                                            {
                                                playerX -= 2;//碰到boss后被拉回去
                                            }
                                            break;

                                        case 'j':
                                        case 'J':
                                            if ((playerX == bossX && playerY == bossY - 1 ||
                                               playerX == bossX && playerY == bossY + 1 ||
                                               playerX == bossX - 2 && playerY == bossY ||
                                               playerX == bossX + 2 && playerY == bossY) && bossHP > 0)
                                            {
                                                fight = true;
                                                Console.SetCursorPosition(3, h - 5);
                                                Console.WriteLine("战斗开始，按j继续");
                                                Console.SetCursorPosition(3, h - 4);
                                                Console.WriteLine("Charlie血量为{0}", playerHP);
                                                Console.SetCursorPosition(3, h - 3);
                                                Console.WriteLine("boss血量为{0}", bossHP);
                                                
                                            }
                                            break;
                                    }
                                }
                                if(over)
                            {
                                break;
                            }
                        
                        }
                        break;                       
                    case 3:
                        Console.Clear();//同上
                        Console.SetCursorPosition(w / 2 - 10, 8);
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("挑战成功！游戏结束！");
                        //给开始界面构造一个循环
                        int number2 = 0;
                        //bool over=false;
                        bool exit2 = false;
                        //用一个标识  来处理 想要在while循环内部的switch逻辑执行时 希望退出外层while循环时改变标识即可
                        while (true)
                        {
                            //显示  内容
                            //先设置光标再显示内容
                            Console.ForegroundColor = number2 == 0 ? ConsoleColor.DarkBlue : ConsoleColor.White;//三目运算符
                            Console.SetCursorPosition(w / 2 - 4, 11);
                            Console.Write("回到开始");
                            Console.ForegroundColor = number2 == 1 ? ConsoleColor.DarkBlue : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 13);
                            Console.Write("结束游戏");
                            char input = Console.ReadKey(true).KeyChar;
                            switch (input)
                            {
                                case 'w':
                                case 'W':
                                    --number2;
                                    if (number2 < 0)
                                    {
                                        number2 = 0;
                                    }
                                    break;
                                case 's':
                                case 'S':
                                    ++number2;
                                    if (number2 > 1)
                                    {
                                        number2 = 1;
                                    }
                                    break;
                                case 'j':
                                case 'J':
                                    if (number2 == 0)
                                    {
                                        //改变当前选择的场景编号
                                        scene = 1;
                                        //退出内层循环
                                        exit2 = true;
                                    }
                                    else
                                    {
                                        Environment.Exit(0);
                                    }
                                    break;
                            }
                            if (exit2)
                            {
                                break;
                            }
                        }
                     break ;
                }  
            }
        }
    }
}
